<template>
	<view>
		<!-- 加载中 -->
		<view
			v-show="show"
			class="position-fixed top-0 left-0 bottom-0 right-0 flex align-center justify-center bg-light"
			style="z-index: 9999;"
		>
			<text class="font text-muted">加载中....</text>
		</view>
		<view style="height:40px;"></view>
		<view
			class="flex justify-center align-center font-lg text-muted"
			style="margin-top: 100rpx;margin-bottom: 80rpx;"
		>
			欢迎回来
		</view>
		<view class="px-4">
			<input
				type="text"
				v-model="form.username"
				placeholder="手机号/邮箱/账户"
				class="uni-input rounded bg-light mb-4"
			/>
			<input
				type="password"
				v-model="form.password"
				placeholder="请输入密码"
				class="uni-input rounded bg-light mb-4"
			/>
			<input
				v-if="type !== 'login'"
				type="password"
				v-model="form.repassword"
				placeholder="请确认密码"
				class="uni-input rounded bg-light mb-4"
			/>
			<view
				class="bg-primary text-white rounded-circle flex align-center justify-center font-lg"
				@click="submit"
			>
				{{ type === 'login' ? '登 录' : '注 册' }}
			</view>
			<view class="flex align-center justify-center pt-5">
				<view class="text-muted mx-2 font-sm " style="cursor: pointer;" @click="changType">
					{{ type === 'login' ? '注 册' : '登 录' }}
				</view>
			</view>
		</view>
	</view>
</template>

<script>
export default {
	data() {
		return {
			type: 'login',
			form: {
				username: '',
				password: '',
				repassword: ''
			},
			show: false
		}
	},
	onLoad() {
		this.show = true
		if (!this.$store.state.token) {
			this.show = false
			uni.showToast({
				title: '请先登录!',
				icon: 'none'
			})
			return
		}
		uni.switchTab({
			url: '../index/index'
		})
	},
	methods: {
		changType() {
			this.type = this.type === 'login' ? 'reg' : 'login'
		},
		submit() {
			let msg = this.type === 'login' ? '登录' : '注册'
			this.$H.post('/' + this.type, this.form).then(res => {
				uni.showToast({
					title: msg + '成功',
					icon: 'none'
				})

				if (this.type === 'login') {
					this.$store.dispatch('login', res).then(res => {
						uni.switchTab({
							url: '../index/index'
						})
						this.$store.dispatch('getShareUrl')
					})
				} else {
					this.form = {
						username: '',
						password: '',
						repassword: ''
					}
					this.changType()
				}
			})
		}
	}
}
</script>

<style></style>
